﻿Imports System.Xml.Serialization
Imports System.Text.RegularExpressions
REM 权限枚举
Public Enum ACL
    All = 0
    Passport = 1
    ValidCode = 2
    Admin = 3
End Enum

Public Class PageConfig
    Inherits DefaultConfig
    Private Shared mConfigInfo As LabelConfigInfo
    Private Shared mConfigFilePath As String
    Private Shared mFileDateTime As DateTime
    Private Shared mRawUrl As String
    Private Shared mHtmlFile As String


    REM 配置文件路径
    Public Overloads Shared ReadOnly Property ConfigFilePath(ByVal fnHtmlFile As String) As String
        Get
            Return mConfigFilePath
        End Get
    End Property

    Public Shared Function Load(ByVal fnPath As String) As LabelConfigInfo
        If IO.File.Exists(fnPath) Then
            mFileDateTime = IO.File.GetLastWriteTime(fnPath)
            Try
                If mConfigInfo Is Nothing Then mConfigInfo = New LabelConfigInfo
                mConfigInfo = CType(LoadConfig(mFileDateTime, fnPath, mConfigInfo, False), LabelConfigInfo)
            Catch ex As Exception
                mErrMessage = ex.Message
                mConfigInfo = New LabelConfigInfo
            End Try
        Else
            mConfigInfo = New LabelConfigInfo
        End If
        Return mConfigInfo
    End Function

    Protected Overrides Sub Finalize()
        mConfigInfo = Nothing
        MyBase.Finalize()
    End Sub
End Class

''' <summary>
''' 页面数据配置类
''' </summary>
''' <remarks></remarks>
''' 
<Serializable()> <XmlRoot("Labels")> Public Class LabelConfigInfo
    Implements IConfigInfo
    ''' <summary>
    ''' 打开调试功能
    ''' </summary>
    ''' <remarks></remarks>
    Public Debug As Boolean = False
    ''' <summary>
    ''' 页面编码
    ''' </summary>
    ''' <remarks></remarks>
    Public CharSet As String = String.Empty
    ''' <summary>
    ''' 缓存时间
    ''' </summary>
    ''' <remarks></remarks>
    Public CacheTime As Integer

    ''' <summary>
    ''' 页面缓存控制
    ''' </summary>
    ''' <remarks></remarks>s
    Public CacheControl As String

    ''' <summary>
    ''' 允许上传文件类型
    ''' </summary>
    ''' <remarks></remarks>
    Public AllowUploadTypes As String

    ''' <summary>
    ''' 最大上传文件大小,默认200,单位：K
    ''' </summary>
    ''' <remarks></remarks>
    Public MaxUploadSize As Integer = 0

    ''' <summary>
    ''' 上传路径
    ''' </summary>
    ''' <remarks></remarks>
    Public UploadPath As String

    ''' <summary>
    ''' 上传图片缩略图
    ''' </summary>
    ''' <remarks></remarks>
    Public ThumbnailPath As String

    ''' <summary>
    ''' 最小画布
    ''' </summary>
    ''' <remarks></remarks>
    Public MinCanvas As String


    ''' <summary>
    ''' 图片质量
    ''' </summary>
    ''' <remarks></remarks>
    Public Quality As Integer

    ''' <summary>
    ''' 水印位置
    ''' </summary>
    ''' <remarks></remarks>
    Public Watermarkposition As String


    ''' <summary>
    ''' 水印透明度
    ''' </summary>
    ''' <remarks></remarks>
    Public Watermarkalpha As Single


    ''' <summary>
    ''' 画布大小
    ''' </summary>
    ''' <remarks></remarks>
    Public Canvas As String

    ''' <summary>
    ''' 缩略图
    ''' </summary>
    ''' <remarks></remarks>
    Public Thumbnail As String = ""

    ''' <summary>
    ''' 页面压缩控制
    ''' </summary>
    ''' <remarks></remarks>
    Public Compression As String

    ''' <summary>
    ''' 权限控制
    ''' </summary>
    ''' <remarks></remarks>
    Public ACL As Integer


    ''' <summary>
    ''' 关键字栏截模式
    ''' </summary>
    ''' <remarks></remarks>
    Public KWInterceptMethod As Integer = -1

    ''' <summary>
    ''' 关键字记录模式
    ''' </summary>
    ''' <remarks></remarks>
    Public KWRecordMethod As Integer = -1


    ''' <summary>
    ''' 页面安全权限
    ''' </summary>
    ''' <remarks></remarks>
    <XmlElement("Security")> Public Security() As ParamItem = {}

    Public MID As Integer = 0
    Public DefACL As String

    REM 是否缓存
    Public ReadOnly Property EnableCache() As Boolean
        Get
            Return (CacheTime > 0)
        End Get
    End Property

    Private CurrentName As String
    <XmlElement("List")> Public Items() As LabelItem = {}
    <XmlElement("Table")> Public Tables() As TableItem = {}

    REM 查找数组
    Public Function Find(ByVal Name As String) As LabelItem
        CurrentName = Name
        If Items Is Nothing OrElse Items.Length = 0 Then Return Nothing
        Return Array.Find(Items, AddressOf FindWhere)
    End Function

    Private Function FindWhere(ByVal item As LabelItem) As Boolean
        Return item.Name = CurrentName
    End Function

    ''' <summary>
    ''' 查找数据表
    ''' </summary>
    ''' <param name="Name">数据表名</param>
    ''' <returns>表对象</returns>
    ''' <remarks></remarks>
    Public Function FindTable(ByVal Name As String) As TableItem
        CurrentName = Name
        If Items Is Nothing OrElse Items.Length = 0 Then Return Nothing
        Return Array.Find(Tables, AddressOf FindTableWhere)
    End Function

    Private Function FindTableWhere(ByVal item As TableItem) As Boolean
        Return item.Name = CurrentName
    End Function

    Protected Overrides Sub Finalize()
        Items = Nothing
        MyBase.Finalize()
    End Sub
End Class

''' <summary>
''' 选项
''' </summary>
''' <remarks></remarks>
<Serializable()> Public Class LabelItem
    Public ID As Integer
    Public UriSplitChar As String
    Public Name As String
    Public Paramaters As String
    Public Variables As String
    Public Table As LabelTable
    Public XsltStyle As String
    Public XsltModule As String
    Public ACL As String
    Public XsltCacheTime As Integer
End Class

''' <summary>
''' 上传配置类
''' </summary>
''' <remarks></remarks>
<Serializable()> Public Class UploadControl
    ''' <summary>
    ''' 允许上传文件类型
    ''' </summary>
    ''' <remarks></remarks>
    Public AllowUploadTypes As String

    ''' <summary>
    ''' 最大上传文件大小,默认200,单位：K
    ''' </summary>
    ''' <remarks></remarks>
    Public MaxUploadSize As Integer = 0

    ''' <summary>
    ''' 最小图像尺寸
    ''' </summary>
    ''' <remarks></remarks>
    Public MinCanvas As String

    ''' <summary>
    ''' 图片质量
    ''' </summary>
    ''' <remarks></remarks>
    Public Quality As Integer = 88

    ''' <summary>
    ''' 画布大小
    ''' </summary>
    ''' <remarks></remarks>
    Public Canvas As String

    ''' <summary>
    ''' 上传路径
    ''' </summary>
    ''' <remarks></remarks>
    Public UploadPath As String

    ''' <summary>
    ''' 上传图片缩略图
    ''' </summary>
    ''' <remarks></remarks>
    Public ThumbnailPath As String

    ''' <summary>
    ''' 水印模式
    ''' </summary>
    ''' <remarks></remarks>
    Public WaterMarkType As Integer = -1

    ''' <summary>
    ''' 水印图透明度
    ''' </summary>
    ''' <remarks></remarks>
    Public Watermarkalpha As Single = -1

    ''' <summary>
    ''' 水印文字
    ''' </summary>
    ''' <remarks></remarks>
    Public WaterMarkText As String

    ''' <summary>
    ''' 缩略模式
    ''' </summary>
    ''' <remarks>0,101,102,200</remarks>
    Public ThumbnailMode As Integer = -1

    ''' <summary>
    ''' 缩略图宽度,单位:像素
    ''' </summary>
    ''' <remarks>像素单位</remarks>
    Public ThumbnailWidth As Integer = -1

    ''' <summary>
    ''' 缩略图高度,单位:像素
    ''' </summary>
    ''' <remarks>像素单位</remarks>
    Public ThumbnailHeight As Integer = -1

    Public Position As String
End Class

<Serializable()> Public Class LabelTable
    <XmlAttribute()> Public type As String = "table"
    ''' <summary>
    ''' 表名或是存储过程名称
    ''' </summary>
    ''' <remarks></remarks>
    Public Name As String
    ''' <summary>
    ''' 每页最大记录条数
    ''' </summary>
    ''' <remarks></remarks>
    Public MaxOnPage As String = "20"
    ''' <summary>
    ''' 当前页码
    ''' </summary>
    ''' <remarks></remarks>
    Public CurPage As String = "1"
    ''' <summary>
    ''' 查询返回的字段列表 如select a,b,c
    ''' </summary>
    ''' <remarks></remarks>
    Public Query As String
    ''' <summary>
    ''' 排序条件
    ''' </summary>
    ''' <remarks></remarks>
    Public OrderBy As String
    ''' <summary>
    ''' 附加的变量值
    ''' </summary>
    ''' <remarks></remarks>
    Public Variables As String
    ''' <summary>
    ''' 条件参数数据
    ''' </summary>
    ''' <remarks></remarks>
    <XmlElement("Where")> Public Where() As WhereItem
    ''' <summary>
    ''' 存储过程参数数组
    ''' </summary>
    ''' <remarks></remarks>
    <XmlElement("Param")> Public Param() As ParamItem       'type=proc 时此参数才有效

End Class

<Serializable()> Public Class ParamItem
    ''' <summary>
    ''' 参数名称
    ''' </summary>
    ''' <remarks></remarks>
    <XmlAttribute()> Public name As String
    ''' <summary>
    ''' 参数名称
    ''' </summary>
    ''' <remarks></remarks>
    <XmlAttribute()> Public field As String
    ''' <summary>
    ''' 参数正则验证的条件
    ''' </summary>
    ''' <remarks></remarks>
    <XmlAttribute()> Public regex As String
    ''' <summary>
    ''' 正则验证不通过，返回的信息
    ''' </summary>
    ''' <remarks></remarks>
    <XmlAttribute()> Public message As String
    ''' <summary>
    ''' 导入的参数值
    ''' </summary>
    ''' <remarks></remarks>
    <XmlText()> Public Value As String
End Class

<Serializable()> Public Class WhereItem
    <XmlAttribute()> Public field As String
    ''' <summary>
    ''' 是否为空
    ''' </summary>
    ''' <remarks></remarks>
    <XmlAttribute()> Public isnull As Boolean = False
    ''' <summary>
    ''' 前括号
    ''' </summary>
    ''' <remarks></remarks>
    <XmlAttribute()> Public before As Integer = 0           '前括号
    ''' <summary>
    ''' 后括号
    ''' </summary>
    ''' <remarks></remarks>
    <XmlAttribute()> Public after As Integer = 0            '后括号
    ''' <summary>
    ''' 条件逻辑
    ''' </summary>
    ''' <remarks></remarks>
    <XmlAttribute()> Public andor As String = "and"
    ''' <summary>
    ''' 条件表达式
    ''' </summary>
    ''' <remarks></remarks>
    <XmlAttribute()> Public mode As String = "="            ' 等于
    ''' <summary>
    ''' like模式的格式
    ''' </summary>
    ''' <remarks>Mode 为like 才有效({0}代表值)</remarks>
    <XmlAttribute()> Public pattern As String = "%{0}%"     ' Mode 为like 才有效
    ''' <summary>
    ''' 条件格式
    ''' </summary>
    ''' <remarks>{0}代表值</remarks>
    <XmlAttribute()> Public format As String = " '{0}' "      ' 格式化
    ''' <summary>
    ''' 内容检查
    ''' </summary>
    ''' <remarks></remarks>
    <XmlAttribute()> Public regex As String = ""               ' 内容比对
    ''' <summary>
    ''' 最小值
    ''' </summary>
    ''' <remarks></remarks>
    <XmlElement()> Public MinValue As String
    ''' <summary>
    ''' 最大值
    ''' </summary>
    ''' <remarks></remarks>
    <XmlElement()> Public MaxValue As String
    ''' <summary>
    ''' 值
    ''' </summary>
    ''' <remarks></remarks>
    <XmlText()> Public Value As String
End Class









